package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Teacher;

public class TeacherDao {
    //建立存储数组
    private Teacher[] teachers = new Teacher[5];

    //查找老师
    public Teacher[] findAllTeacher() {
        return teachers;
    }

    //添加老师
    public boolean addTeacher(Teacher teacher) {
        //需要判断数组是否已满
        // 标记位flag
        boolean flag = false;
        //标记索引值index
        int index = -1;
        for (int i = 0; i < teachers.length; i++) {
            if (teachers[i] == null) {
                index = i;
                break;
            }
        }
        //如果未满则是索引值,已满则是-1
        if (index != -1) {
            flag = true;
            teachers[index] = teacher;
        }
        return flag;
    }

    //删除老师方法
    public void delateTeacherById(String delId) {
        //获得索引值以对数组进行操作
        int index = getIndex(delId);
        teachers[index] = null;
    }

    //获取索引方法
    public int getIndex(String delId) {
        int index = -1;
        //遍历数组得到索引值
        for (int i = 0; i < teachers.length; i++) {
            if (teachers[i] != null && teachers[i].getId().equals(delId)) {
                index = i;
                break;
            }
        }
        return index;
    }

    //修改老师方法
    public void updateTeacher(Teacher teacher) {
        //获得修改索引
        int index = getIndex(teacher.getId());
        //进行覆盖
        teachers[index] = teacher;
    }
}
